package com.tian.comsumer;

import com.alibaba.fastjson.JSON;
import com.rabbitmq.client.Channel;
import com.tian.constant.RabbitMQConstant;
import com.tian.constant.RabbitMqQueueConstant;
import com.tian.dto.message.ChargeSystemMessageAddReqDto;
import com.tian.message.ChargeSystemMessageSendMessage;
import com.tian.message.UserPointMessage;
import com.tian.service.ChargeSystemMessageMQService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.IOException;

/**
 * {@code @description:} 站内信发送
 *
 * @author tianwc 公众号：Java后端技术全栈
 * 在线刷题 1200+java面试题和1000+篇技术文章：<a href="https://woaijava.cc/">博客地址</a>
 * {@code @date:} 2024-08-12 11:35
 * {@code @version:} 1.0
 */
@RabbitListener(queues = RabbitMQConstant.SYS_MESSAGE_QUEUE)
@Component
@Slf4j
public class ChargeSystemMessageConsumer {
    @Resource
    private ChargeSystemMessageMQService chargeSystemMessageMQService;

    @RabbitHandler
    public void process(Object data, Channel channel, Message message) throws IOException {
        log.info("ChargeSystemMessageConsumer 消费者消费消息：{}", data);
        ChargeSystemMessageSendMessage chargeSystemMessageSendMessage = JSON.parseObject(new String(message.getBody()), ChargeSystemMessageSendMessage.class);
        try {
            Boolean result = chargeSystemMessageMQService.add(chargeSystemMessageSendMessage);
            if (result) {
                channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
                return;
            }
            channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, false);
        } catch (Exception e) {
            log.error("ChargeSystemMessageConsumer 消费者消费消息异常：{}", e.getMessage());
            channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, false);
        }
    }
}
